package com.teste.websocket;

import java.io.IOException;
import java.util.Set;

import javax.websocket.CloseReason;
import javax.websocket.EncodeException;
import javax.websocket.EndpointConfig;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;

import com.teste.websocket.decoder.MessageDecoder;
import com.teste.websocket.encoder.MessageEncoder;
import com.teste.websocket.model.Message;

@ServerEndpoint(
		value="/mapa/{room-name}"/*, 
		encoders = {MessageEncoder.class},
		decoders = {MessageDecoder.class}*/)
public class MapaWebSocket {
	@OnMessage
	public void onMessage(Session session, String msg, @PathParam("room-name") String roomName) {
		try {
			System.out.println(roomName);
			Set<Session> openSessions = session.getOpenSessions();
			for (Session s : openSessions) {
				if(!s.equals(session)) {
					s.getBasicRemote().sendText(msg);
				}
			}
			System.out.println(msg.toString());
		} catch (IOException e) {
			e.printStackTrace();
		} /*catch (EncodeException e) {
			e.printStackTrace();
		}*/
	}

	@OnOpen
	public void open(Session session, EndpointConfig conf, @PathParam("room-name") String roomName) {
		System.out.println("opa");
	}

	@OnError
	public void error(Session session, Throwable error) {

	}

	@OnClose
	public void close(Session session, CloseReason reason) {

	}
}
